/**
 * user controller js code
 */
define(function(require, exports) {

	//加载依赖
	require("ajaxproxy");
	require("areaSelect");
	require("jpreview");
	var common = require("common");
	var template = require("jtemplate");
	template.config("openTag", "{[");
	template.config("closeTag", "]}");

	exports.init = function(configs) {
		//初始化AjaxProxy插件
		$('.ajaxproxy').AjaxProxy({
			dataType : "json",
			successCode : "000", //成功时返回的code
			method : "post",
			formId : "content-add-form",
			formCheckHandler : function(form_id) {
				var __form = new JForm({
					formId : form_id,
					continueCheck : false,
					showMessage : function( type, message, ele ) { //错误处理接口
						ele.focus();
						var errorElements = $(ele).parents(".form-group").next();
						if ( !errorElements.hasClass('help-block') ) {
							errorElements = $(ele).parents(".form-group").find(".help-block");
						}
						if ( type == "error" ) {
							errorElements.addClass("text-error").html(message);
							//var $panel = $(ele).parents(".tab-pane");
							//if ( !$panel.hasClass("active") ) {
							//	var id = $panel.attr("id");
							//	$('a[href="#'+id+'"]').tab('show');
							//}
						} else {
							errorElements.removeClass("text-error").empty();
						}
					}
				});

				//设置报错提示信心
				__form.setMessage(__global.jformErrorMessage);

				return __form.checkFormData();
			},
			callbackDelay : __global.jdialog.timer,
			timeInterval : __global.jdialog.timer,
			callBefore : function() {
				return true;
			},
			callBack : function (data) {   /* 执行ajax之后的回调函数 */
				common.ajaxCallback(data);
			}
		});

		//初始化区域选择器
		$("#area-select-box").JAreaSelect({
			prov : configs.prov,
			city : configs.city,
			dist : configs.dist,
			name : {
				prov : 'company[prov]',
				city : 'company[city]',
				dist : 'company[dist]'
			}
		});

		//初始化图片预览插件
		$(".img-view").jpreview();

		//三证合一选中事件
		$("#three_in_one").on("ifChecked", function() {
			$("#orgcode").hide();
			$("#taxno").hide();
			$('input[name="company[orgcode_pic]"]').removeAttr("required");
			$('input[name="company[orgcode]"]').removeAttr("required");
			$('input[name="company[taxno_pic]"]').removeAttr("required");
			$('input[name="company[taxno]"]').removeAttr("required");
		}).on("ifUnchecked", function() {
			$("#orgcode").show();
			$("#taxno").show();
			$('input[name="company[taxno]"]').attr("required", "true");
			$('input[name="company[taxno_pic]"]').attr("required", "true");
			$('input[name="company[orgcode_pic]"]').attr("required", "true");
			$('input[name="company[orgcode]"]').attr("required", "true");
		});

		//初始化选项卡
		$('#myTab a:first').tab('show');

		//初始化tooltip
		$('[data-toggle="tooltip"]').tooltip({
			trigger : "hover"
		});

		//初始化swich插件
		common.bswitctListInit(configs);
		common.bswitchAddInit();
		common.checkExists(configs); //自动检测手机号码是否存在

		common.searchInit({}); //初始化搜索表单

	}

	//修改会员号
	exports.modifyMobile = function(configs) {
		$(".modify-mobile").on("click", function() {
			var userid = $(this).data('id');
			common.post("修改会员号", "modify-mobile-template", {width:600, height:230}, {id:userid}, configs.modifyMobile_url);
		});
	}

	//修改密码
	exports.modifyPass = function(configs) {
		$(".modify-pass").on("click", function() {
			var userid = $(this).data('id');
			common.post("修改会员密码", "modify-pass-template", {width:600, height:340}, {id:userid}, configs.modifyPass_url);
		});
	}

	//会员认证审核
	exports.check = function(configs) {

		$("#uncheck-btn").on('click', function() {
			var html = template("user-check-template", {});
			var options = {
				title : "补充原因",
				content : html,
				icon : 'none',
				width : 600,
				height : 0,
				top : 0,
				container : '#right-section',
				button : {
					'保存' : function() {

						if ( !common.formCheckHandler('user-check-form', false) ) return false;

						popWin.lock();
						//设置审核不通过
						$('input[name="user[ischecked]"]').val(3);
						var checkNote = $("#check-note").val().trim();
						$("#content-add-form").append('<input type="hidden" name="user[check_note]" value="'+checkNote+'" />');

						$("#check-btn").trigger("click");
						popWin.close();

					},
					'取消' : function() {
						popWin.close();
					}
				}
			}
			var options = $.extend(__global.jdialog, options);
			var popWin = JDialog.win.work(options);
		});

	}

	//初始化上传控件
	exports.upload = function() {

		require('jupload');
		//上传身份正面
		$(".jupload").each(function(idx, ele) {
			$(ele).JUpload({
				url : "/utils/upload/doUpload",  //后台上传路径
				max_filenum : 1,
				src : "src",
				onSuccess : function(data) {
					$(ele).next().val(data);
					$(ele).next().next().attr("data-src", data);
					common.message({
						type : 'ok',
						top : __global.jdialog.top,
						content : '图片上传成功'
					});
				}
			})
		});

	}
});
